<?php

namespace App\Http\Controllers\Backend\Setting;

use App\Http\Requests\Backend\Setting\SettingBannerImageRequest;
use App\Models\SettingLog;
use App\Models\SettingBannerImage;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class SettingBannerImageController extends Controller
{
    use \App\Library\Traits\SettingLog;
    const SETTING_TABLE = 'setting_banner_images';

    public function __construct()
    {
        $this->middleware('auth:backend');
        $this->user = auth('backend')->user();
    }

    /**
     * Description:扒图列表
     * Author: wjl
     * DateTime: 2019-04-10 18:26
     * @return mixed
     */
    public function index(Request $request)
    {
        //扒图列表
        $page_size = $request->get('page_size', 10);

        $setting_home = new SettingBannerImage();
        $setting_home->setConnection('backend');
        $list = $setting_home->select('id', 'img_url', 'is_show', 'url', 'title')->orderBy('id', 'desc')->paginate($page_size);

        if (!empty($list)) {
            $info = [];
            foreach ($list as $v) {
                $info[] = [
                    'id' => $v->id,
                    'img_url' => getOssUrl($v->img_url),
                    'img_path' => $v->img_url,
                    "is_show" => $v->is_show,
                    "url" => $v->url,
                    "title" => $v->title
                ];
            }
            return resp(['data' => $info, 'total' => $list->total()]);
        }

        return success($list);
    }


    /**
     * Description: 详情
     * Author: wjl
     * DateTime: 2019-04-11 09:34
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $setting_home = new SettingBannerImage();
        $setting_home->setConnection('backend');
        $imageInfo = $setting_home->select('id', 'img_url', 'is_show', 'url', 'title')->where('id', $id)->first();
        if ($imageInfo) {
            $imageInfo["img_url"] = getOssUrl($imageInfo['img_url']);
            $imageInfo["img_path"] = $imageInfo['img_url'];
            return success($imageInfo);
        }
        return failed('当前信息不存在');
    }

    /**
     * Description:
     * Author: wjl
     * DateTime: 2019-04-11 19:59
     * @param SettingBannerImageRequest $request
     * @return mixed
     */
    public function store(SettingBannerImageRequest $request)
    {
        $updateData = $request->only(['img_path', 'title', 'url']);

        $banner_image = new SettingBannerImage();


        $banner_image->img_url = $updateData['img_path'];

        if (isset($updateData['url'])) {
            $banner_image->title = $updateData['title'];
            $banner_image->url = $updateData['url'];
        }
        $info = $banner_image->save();

        return $info ? stored('添加成功') : failed('添加失败');
    }

    /**
     * Description:更新
     * Author: wjl
     * DateTime: 2019-04-10 18:24
     * @param SettingBannerImageRequest $request
     * @param $id
     * @return mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function update(SettingBannerImageRequest $request, $id)
    {
        $updateData = $request->only(['img_path',  'url','is_show', 'title']);
        if(isset($updateData['img_path'])) {
            $updateData['img_url'] = $updateData['img_path'];
            unset($updateData['img_path']);
        }

        if (isset($updateData['url'])) {
            $updateData['url'] = trim($updateData['url']);
        }

        if (isset($updateData['title'])) {
            $updateData['title'] = trim($updateData['title']);
        }

        $info = SettingBannerImage::where('id', $id)->update($updateData);

        //只能应用一个
        if (isset($updateData['is_show'])) {
            $status = intval($updateData['is_show']);
            if ($status == 1) {
                SettingBannerImage::where('id', '<>', $id)->update(['is_show' => 2]);
            }
        }

        if ($info) {

           // $this->setting_log($this->user->id, self::SETTING_TABLE, $id, SettingLog::LOG_UPDATE, $updateData);
            return updated('');
        }
        return failed('更新失败');
    }

    /**
     * Description: 删除图片
     * Author: wjl
     * DateTime: 2019-04-10 18:23
     * @param $id
     * @return mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function destroy($id)
    {
        $result = SettingBannerImage::where('id', $id)->delete();

        if ($result) {
            $this->setting_log($this->user->id, self::SETTING_TABLE, $id, SettingLog::LOG_DELETE);
            return deleted();
        } else {
            return failed('删除失败');
        }
    }
}
